/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.interfaces;

import goodenglish.infrastructure.Config;
import goodenglish.infrastructure.PropUtil;
import goodenglish.infrastructure.repository.db.DbCreater;
import goodenglish.interfaces.videoplay.PlayMode;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 程序设置
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class SettingPane extends VBox {

	private static Log logger = LogFactory.getLog(SettingPane.class);
	public static TextField studyTimeText;
	public static TextField stepText;
	public static TextField replayTotalCountText;
	public static TextField dictPathText;
	public static TextField dictNameText;
	private RadioButton sutdySubtRb;
	private RadioButton directPlayRb;

	public SettingPane() {
		this.setSpacing(Config.VGAP);
		// 添加菜单
		ToggleGroup tg = new ToggleGroup();
		sutdySubtRb = new RadioButton("字幕学习播放");
		sutdySubtRb.setToggleGroup(tg);
		if (Config.PLAY_MODE.equals(PlayMode.STUDY_SUBT)) {
			sutdySubtRb.setSelected(true);
		}

		this.getChildren().add(sutdySubtRb);
		addStudySubtSeting();

		directPlayRb = new RadioButton("直接播放，字幕显示意思");
		if (Config.PLAY_MODE.equals(PlayMode.DIRECT)) {
			directPlayRb.setSelected(true);
		}
		directPlayRb.setToggleGroup(tg);
		this.getChildren().add(directPlayRb);
		addDictSetting();
		addSave();
		// 清空所有用户数据
		Button saveBtn = new Button("清空用户数据");
		saveBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				DbCreater.reCreateDb();
				Main.instance.showModalMessage(new MessageDialog("清空完成"));
			}

		});
		this.getChildren().add(saveBtn);

	}

	private void addSave() {
		Button saveBtn = new Button("应用");
		saveBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				if (sutdySubtRb.isSelected()) {
					Config.alterPLAY_MODE(PlayMode.STUDY_SUBT);
				}
				if (directPlayRb.isSelected()) {
					Config.alterPLAY_MODE(PlayMode.DIRECT);

				}
				if (studyTimeText.getText().trim().matches("[0-9]*")) {
					Config.alterPLAYLISTENER_STUDYTIME(Integer.parseInt(studyTimeText.getText().trim()));

				}
				if (stepText.getText().trim().matches("[0-9]*")) {
					;
					Config.alterPLAYLISTENER_STEP(Integer.parseInt(stepText.getText().trim()));
				}

				if (replayTotalCountText.getText().trim().matches("[0-9]*")) {
					Config.alterPLAYLISTENER_REPLAYCOUNT(Integer.parseInt(replayTotalCountText.getText().trim()));
				}
				Config.DICT_PATH = dictPathText.getText();
				PropUtil.storeProperty("DICT_PATH", Config.DICT_PATH);
				Config.DICT_NAME = dictNameText.getText();
				PropUtil.storeProperty("DICT_NAME", Config.DICT_NAME);
			}

		});
		this.getChildren().add(saveBtn);
	}

	private void addDictSetting() {
		this.getChildren().add(new Label("字典设置"));
		HBox hBox = new HBox();
		hBox.getChildren().add(new Label("字典路径"));
		dictPathText = new TextField(Config.DICT_PATH);
		hBox.getChildren().add(dictPathText);
		dictNameText = new TextField(Config.DICT_NAME);
		hBox.getChildren().add(new Label("字典名称"));
		hBox.getChildren().add(dictNameText);
		this.getChildren().add(hBox);
	}

	private void addStudySubtSeting() {
		HBox hBox = new HBox();
		studyTimeText = new TextField();
		studyTimeText.setText("" + Config.PLAYLISTENER_STUDYTIME);
		stepText = new TextField();
		stepText.setText("" + Config.PLAYLISTENER_STEP);
		replayTotalCountText = new TextField();
		replayTotalCountText.setText("" + Config.PLAYLISTENER_REPLAYCOUNT);

		hBox.getChildren().add(new Label("每次字幕学习个数"));
		hBox.getChildren().add(stepText);
		hBox.getChildren().add(new Label("每个单词学习时间"));
		hBox.getChildren().add(studyTimeText);
		hBox.getChildren().add(new Label("重播放次数"));
		hBox.getChildren().add(replayTotalCountText);
		this.getChildren().add(hBox);
	}

}
